package com.scoy.honeymei.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.CartAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.CartBean;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @ClassName: ShopCartFragment$
 * @Description: 首页 购物车界面
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 9:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 9:20$
 * @UpdateRemark: 更新内容
 */
public class ShopCartFragment extends BaseFragment {

    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    @BindView(R.id.hcf_allcheck_tv)
    TextView hcfAllcheckTv;
    @BindView(R.id.hcf_allmoney_tv)
    TextView hcfAllmoneyTv;
    @BindView(R.id.hcf_settle_tv)
    TextView hcfSettleTv;
    private Context mContext;
    private Unbinder unbinder;
    private ArrayList<CartBean> datalist;
    private CartAdapter cartAdapter;
    private int allCheck;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cart, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();


        initNormal();


        httpCartList();

        return view;
    }

    @Override
    public void initNormal() {
        datalist = new ArrayList<>();

        hcfAllmoneyTv.setText("￥0.00");

        initRV();

        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            datalist.clear();
            httpCartList();

        }).setEnableLoadMore(false);
    }

    private void initRV() {
        cartAdapter = new CartAdapter(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(cartAdapter);

        //shop select
        cartAdapter.setOnOneClick(pos -> {
            CartBean cartBean = datalist.get(pos);
            List<GoodsOrderBean> goodsList = cartBean.getGoodsList();

            if (cartBean.getShopselect() == 1) {
                cartBean.setShopselect(0);
                cartAdapter.notifyItemChanged(pos, "uncheckshop0");
                for (GoodsOrderBean goodsBeaan : goodsList) {
                    goodsBeaan.setGoodsselect(0);
                }
            } else {
                cartBean.setShopselect(1);
                cartAdapter.notifyItemChanged(pos, "checkshop0");
                for (GoodsOrderBean goodsBeaan : goodsList) {
                    goodsBeaan.setGoodsselect(1);
                }
            }


        });

        //jian
        cartAdapter.setOnTwoClick((pos, posin) -> {
            List<GoodsOrderBean> goodsList = datalist.get(pos).getGoodsList();
            GoodsOrderBean goodsOrderBean = goodsList.get(posin);
            httpCartChange(goodsOrderBean.getGoodscarid(), 2);
        });

        //add
        cartAdapter.setOnThreeClick((pos, posin) -> {
            List<GoodsOrderBean> goodsList = datalist.get(pos).getGoodsList();
            GoodsOrderBean goodsOrderBean = goodsList.get(posin);
            httpCartChange(goodsOrderBean.getGoodscarid(), 1);
        });

        //selsect
        cartAdapter.setOnFourClick((pos, posin) -> {
            CartBean cartBean = datalist.get(pos);
            List<GoodsOrderBean> goodsList = cartBean.getGoodsList();
            GoodsOrderBean goodsOrderBean = goodsList.get(posin);
            int goodsselect = goodsOrderBean.getGoodsselect();

            boolean allif = true;
            for (GoodsOrderBean goodsBeaan : goodsList) {
                if (goodsselect == 1) {
                    if (goodsBeaan.getGoodsselect() == 0) {
                        allif = false;
                        break;
                    }
                } else {
                    if (goodsBeaan.getGoodsselect() == 1) {
                        allif = false;
                        break;
                    }
                }

            }

            if (goodsselect == 1) {
                if (allif) {
                    cartAdapter.notifyItemChanged(pos, "checkshop");
                    cartBean.setShopselect(1);
                }
            } else {
               /* if(allif){
                    cartAdapter.notifyItemChanged(pos, "uncheckshop");
                    cartBean.setShopselect(0);
                }*/
                if (cartBean.getShopselect() == 1) {
                    cartAdapter.notifyItemChanged(pos, "uncheckshop");
                    cartBean.setShopselect(0);
                }
            }


         /*   int shopselect = cartBean.getShopselect();
            if (shopselect == 0 && allCheck) {
                cartAdapter.notifyItemChanged(pos, "checkshop");
            } else if (shopselect == 0 && !allCheck) {
                cartAdapter.notifyItemChanged(pos, "uncheckshop");
            } else if (shopselect == 1 && !allCheck) {
                cartAdapter.notifyItemChanged(pos, "uncheckshop");
            } else {
                cartAdapter.notifyItemChanged(pos, "checkshop");
            }*/

        });

        //delete
        cartAdapter.setOnFiveClick((pos, posin) -> {
            List<GoodsOrderBean> goodsList = datalist.get(pos).getGoodsList();
            GoodsOrderBean goodsOrderBean = goodsList.get(posin);
            httpCartDelete(goodsOrderBean.getGoodscarid());
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.hcf_allcheck_tv, R.id.hcf_settle_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.hcf_allcheck_tv:
                if (allCheck == 0) {
                    allCheck = 1;
                    hcfAllcheckTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_checked_cart, 0, 0, 0);
                    for (int i = 0; i < datalist.size(); i++) {
                        CartBean cartBean = datalist.get(i);
                        cartBean.setShopselect(1);
                        cartAdapter.notifyItemChanged(i, "checkshop0");
                    }
                } else {
                    allCheck = 0;
                    hcfAllcheckTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_uncheck_cart, 0, 0, 0);
                    for (int i = 0; i < datalist.size(); i++) {
                        CartBean cartBean = datalist.get(i);
                        cartBean.setShopselect(0);
                        cartAdapter.notifyItemChanged(i, "uncheckshop0");
                    }

                }
                break;
            case R.id.hcf_settle_tv:

                break;
        }
    }

    private void httpCartList() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        params.put("page", 0);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.CART_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<CartBean> data = new Gson().fromJson(o, new TypeToken<List<CartBean>>() {
                }.getType());
                datalist.addAll(data);
                cartAdapter.notifyDataSetChanged();
            }
        });

    }

    //删除购物车
    private void httpCartDelete(int ids) {
        HttpParams params = new HttpParams();
        params.put("ids", ids);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.CART_DELETE, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                MyUtil.mytoast(mContext, "删除成功");
                datalist.clear();
                httpCartList();
            }

        });

    }

    //修改购物车数量
    private void httpCartChange(int ids, int status) {
        HttpParams params = new HttpParams();
        params.put("id", ids);
        params.put("status", status);//1为增加 2为减少
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.CART_CHANGE_NUM, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {

            }
        });

    }

}
